(function() {
    //DsSideMenu
    function DsSideMenu(opt) {
        if (!opt || _.isEmpty(opt)) {
            console.log("112233");
            return;
        }
        this.$vue = opt.$vue || {};
        this.treeData = opt.treeData || []; //侧边栏数据
        this.settings = $.extend(true,{
                //参数
                key:"siderBar", // 侧边栏的标识
                xh:0,   // 页面可能存在多个侧标栏
                emptyClass:".zz-not-exist", // 用来创建一个空的jq对象
                tabBarName:"zzTabs",  // 页面切换时需要和tab栏对应???
                pageContentClass:".zz-body", // --- 页面内容区的类名
                pCCSideMenuHide:"sideMenuHide", // --- 侧边栏隐藏时内容区对应的类名
                pCCSideMenuToggle:"sideMenuToggle", // --- 侧边栏折叠时内容区对应的类名
                itemName: "title",
                menuWrapClass: ".ds-navList-wrap", //菜单容器选择器
                menuParentClass: ".ds-navList-template", //
                menuClass: ".ds-navList", //menu类名
                itemClass: ".ds-navList-item", //item类名
                //hasIcon: false, //图标
                iconSrc: "iconSrc", //图标链接的字段名
                activeIconSrc: "activeIconSrc", //选中时的图标链接字段名
                iconClass: ".icon", //图标类名
                //hasArrow: false, //箭头图标
                arrowSrc: "arrowSrc", //箭头链接的字段名
                activeArrowSrc: "activeArrowSrc", //选中时的箭头链接的字段名
                arrowClass: ".arrow", //箭头类名
                tabName: "route", //切换项对应的字段
                activeClass: ".is-active", //item选中时的类名
                toggleClass: "sider-toggle", //菜单折叠时的类名
                scrollClass: "ds-scroll", //菜单滚动条类名
                posTop:64, // 定位距离顶部的距离 50
                pdv:10  // 侧边栏父级内边距
            },
            opt.settings || {}
        );
        this.fns = {
            //事件
            itemClickAfter: $.isFunction(opt.itemClickAfter) ?
                opt.itemClickAfter :
                $.noop, //点击菜单项
            itemIconChange: $.isFunction(opt.itemIconChange) ?
                opt.itemIconChange :
                $.noop, //icon的src修改
            itemArrowChange: $.isFunction(opt.itemArrowChange) ?
                opt.itemArrowChange :
                $.noop //arrow的src修改
        };
        this.tempHtml = ""; //dom字符串
        this.sideMenu = $("#"+this.settings.key+this.settings.xh,"body"); // 侧边栏
        this.sideMenuWidth = 0; // --- 侧边栏的宽度
        this.navListWrap = null; //侧边栏的容器
        this.listWrapTemp = null; // 侧边栏父级
        this.listWrap = null; //侧边栏
        this.sideToggleBtn = this.sideMenu.find(".zz-sider_toggle_btn");
        this.symbolReg = /\-/g; //检查-符号
        this.eleIndexArrOfItem = null; //每个子项对应的ele-index标记(元素)-数组
        this.openIndex = 1; //展开对应菜单时要添加active类名元素的初始下标
        this.clickedControl = false; //点击控制
        // --- 滚动距离--竖向滚动条
        this.NLWH = 0;
        this.LWH = 0;
        this.NLWST = 0;
        this.zzHeaderH = 0;
        this.stepDis = 0;
        // --- 页面tab栏的引用
        this.tabBarObj = null;
        // --- 追踪和当前显示页面匹配的菜单项的元素标记(数组)
        this.eleByMatchPage = [];
    }
    DsSideMenu.prototype = {
        constructor: DsSideMenu,
        listWrap: '<div class="ds-navList"></div>',
        common:{
            //iconSrcLink:"https://lijiek700c.github.io/images/qian.jpg",
            iconSrcLink:"../../../images/zzHeader/logo.png",
            activeIconSrc:"https://lijiek700c.github.io/images/qian.jpg",
            arrowSrcLink:"./images/sideMenu/fj-sider-arrow.png",
            activeArrowSrcLink:"./images/sideMenu/fj-sider-arrow1.png"
        },
        // --- 循环获取tab栏引用
        getTabBarObj(){
            if(this.tabBarObj)return;
            this.tabBarObj = window[this.settings.tabBarName + this.settings.xh];
            _.delay(_.bind(function(){
                !this.tabBarObj && this.getTabBarObj();
            },this),100);
        },
        // 修改location.href对应切换的路径
        refreshLocationHref:function(path){
            //alert(path);
            //alert(location.href);
            //alert(location.pathname);
            //alert(zzGetRealPath());
            //var newPath = path.replace(/\//g,"");
            var path = path || "no-select";
            var pathResult = 'type='+this.settings.xh+'&curPath='+path;
            if(typeof history.pushState === "function"){
                var url = location.pathname + "?"+pathResult
                history.pushState(
                    {
                        url:url,
                        title:document.title
                    },
                    document.title,
                    url
                );
            }else{
                var wt = window.top;
                var location___url = wt.location.href;
                var location___url___search = wt.location.search;
                var search___arr = location___url___search?location___url___search.slice(1).split("="):[];
                //
                if(!location___url___search || newPath!==search___arr[0]){
                    location___url = location___url.replace(/(\?\w+)/g,"");
                    //var mr = (""+ Math.random()*10).replace(/\./g,"");
                    // 存path
                    //$.cookie("zz-old-iframes-path",path);
                    // 修改href
                    //wt.location.hash = "?"+newPath+"="+mr;
                    wt.location.hash = "?"+pathResult;
                }
            }
        },
        // 获取竖向滚动的相关值
        getVerticalValues:function(){
            this.NLWH = this.navListWrap.outerHeight();
            this.LWH = this.listWrapTemp.outerHeight();
            this.NLWST = (this.LWH - this.NLWH) < 0 ? 0 : (this.LWH - this.NLWH);
            //console.log("---this.navListWrap.height---",this.NLWH);
            //console.log("---this.listWrapTemp.height---",this.LWH);
            //console.log("---this.listWrapTemp.st---",this.NLWST);
        },
        // --- 获取侧边栏的宽度
        getSideMenuwidth(){
            this.sideMenuWidth = this.sideMenu.innerWidth();
        },
        // 设置listWrapTemp上下滑动
        listWrapTempSlide(dis){
            this.listWrapTemp.css("transform","translateY("+dis+"px)");
        },
        //
        init: function() {
            if (!this.treeData.length) {
                console.warn("没有数据");
                return;
            }
            //_.template设置
            _.templateSettings = {
                interpolate: /\{\{(.+?)\}\}/g
            };
            // --- 创建侧边栏
            this.initMenu();
            // --- 获取tab栏引用
            this.getTabBarObj();
            return this;
        },
        refreshMenu: function(menuData) {
            //重新创建菜单
            if (!menuData || !_.isArray(menuData)) {
                alert("没有菜单数据");
                return;
            }
            if (this.listWrap && this.navListWrap) {
                this.clearItemsEvents();
                this.FLMenuItem = null;
                this.listWrap.parent().empty();
                this.listWrap = null;
                this.treeData = null;
                this.treeData = menuData;
                //
                this.initMenu();
            }
        },
        initMenu: function() {
            if (_.isArray(this.treeData) && this.treeData.length) {
                //过滤数据
                //var tmpTreeData = this.filterTreeData(this.treeData);
                //this.treeData = null;
                //this.treeData = tmpTreeData;
                //console.log(this.treeData);
                // --- 创建菜单并绑定事件
                this.listWrap = $(this.createMenu(this.treeData));
                //
                this.constructor.prototype.menuClass = this.settings.menuClass;
                this.constructor.prototype.activeClassName = this.settings.activeClass.slice(
                    1
                );
                //
                //var NLW = $(this.settings.menuWrapClass, "body");
                var NLW = this.sideMenu.find(this.settings.menuWrapClass);    
                this.navListWrap = NLW.length > 0 ? NLW : null;
                this.listWrapTemp = this.navListWrap.find(this.settings.menuParentClass);
                this.listWrapTemp.append(this.listWrap);
                // --- 侧边栏宽度
                this.getSideMenuwidth();
                // 获取竖向滚动的相关值
                this.getVerticalValues();
                //设置事件
                this.setItemsEvents();
            } else {
                alert("没有菜单数据");
            }
        },
        filterTreeData: function(data) { //过滤掉数据中hasRemoved=true的选项
            var self = this;
            data = $.grep(data, function(item) {
                //console.log(item);
                if (item.children && $.isArray(item.children) && item.children.length) {
                    item.children = self.filterTreeData(item.children);
                }
                return !item.hasRemoved;
            });
            return data;
        },
        createMenu: function(data, key, eleKey) {
            //创建侧边栏
            if ((_.isArray(data) && !data.length) || !_.isArray(data)) {
                return "";
            }
            var key = key || "";
            var eleKey = eleKey || "";
            var tempHtmlArr = [];
            //
            for (var i = 0; i < data.length; i++) {
                var dataItem = data[i];
                var keyIndex;
                var eleKeyIndex;
                // --- 
                if(dataItem.hidden){
                    //continue;
                }else{
                    var eleKeyIndex;
                    // --- 从当前数据项前面的所有数据项中找出最后一个没有hidden的数据项
                    var ri = 0;
                    for(var k = 0; k < i; k++){
                        if(!data[k].hidden){
                            ri++;
                        }
                    }
                    eleKeyIndex = eleKey ? eleKey + "-" + (ri + 1) : ri + 1 + "";
                    // --- 元素序号标识
                    dataItem.eleIndex = eleKeyIndex;
                }
                // --- 数据项标识
                keyIndex = key ? key + "-" + (i + 1) : i + 1 + "";
                dataItem.index = keyIndex; //数据项添加子级index标记
                tempHtmlArr.push(this.createMenuItem(dataItem, keyIndex, eleKeyIndex));
            }
            return '<div class="ds-navList">' + tempHtmlArr.join("") + "</div>";
        },
        createMenuItem: function(data, key, eleKey) {
            // --- 
            if(data.hidden)return "";
            //创建侧边栏子项
            var tempItemHtmlArr = [];
            var nl = key.search(this.symbolReg) != -1 ? "x" : "1"; //是否是第一级菜单
            var styleStr = data.hasRemoved ? "display:none;" : "";
            var dc = data.children||[];
            var isOnlyFirst = !dc.length?"is-only-first":"";
            tempItemHtmlArr.push(
                _.template(
                    '<div class="ds-navList-item zz-clamp '+isOnlyFirst+'" style="' + styleStr + '" data-level="' +
                    nl +
                    '" data-index="' +
                    key +
                    '" data-ele-index="' +
                    eleKey +
                    '" data-'+this.settings.tabName+'="' +
                    this.setItemTabName(data) +
                    '">' +
                    this.createIconTemp(data, nl) +
                    '<span class="item-tag" title="{{data.' +
                    this.settings.itemName +
                    '}}">{{data.' +
                    this.settings.itemName +
                    "}}</span>" +
                    this.createArrowTemp(data, nl) +
                    "</div>" +
                    this.createMenu(dc, key, eleKey), { variable: "data" }
                )(data)
            );
            return tempItemHtmlArr.join("");
        },
        setItemTabName(data) {
            //给子项标签设置data-route属性
            return data[this.settings.tabName] ? data[this.settings.tabName] : "";
        },
        // 1级菜单项创建图标
        createIconTemp: function(data, flag) {
            var iconSrcLink, // 图标链接
                activeIconSrcLink, // 图标链接--展开
                itemIconSrc, // 菜单数据项--图标链接
                itemIconActiveSrc; // 菜单数据项--图标链接--展开
            itemIconSrc = data[this.settings.iconSrc];
            itemIconActiveSrc = data[this.settings.activeIconSrc];
            // 数据中没有图标链接
            if(!itemIconSrc){
                iconSrcLink = this.common.iconSrcLink;
                return flag == 1 ?
                '<img class="icon" src="' +
                iconSrcLink +
                '" data-noselect="' +
                iconSrcLink +
                '" data-select="' +
                iconSrcLink +
                '" alt="icon">' :
                "";
            }else{
                itemIconActiveSrc = itemIconActiveSrc?itemIconActiveSrc:itemIconSrc;
                return flag == 1 ?
                '<img class="icon" src="' +
                itemIconSrc +
                '" data-noselect="' +
                itemIconSrc +
                '" data-select="' +
                itemIconActiveSrc +
                '" alt="icon">' :
                "";
            }
            return "";
            
        },
        // 1级菜单项创建下拉箭头
        createArrowTemp: function(data, flag) {
            var arrowSrcLink = data[this.settings.arrowSrc]?data[this.settings.arrowSrc]:"";
            //var activeArrowSrcLink = data[this.settings.arrowSrc]?data[this.settings.arrowSrc]:this.common.activeArrowSrcLink;
            // data.children 
            var dc = data.children || [];
            if(!arrowSrcLink){
                return (flag == 1 && dc.length) ?
                '<i class="arrow el-icon-d-arrow-right"></i>':
                "";
            }
            return (flag == 1 && dc.length) ?
                '<img class="arrow" src="' +
                arrowSrcLink +
                '" data-noselect="' +
                arrowSrcLink +
                '" data-select="' +
                activeArrowSrcLink +
                '" alt="arrow">' :
                "";
        },
        // --- 获取数据项层级标记
        getIndexArr: function(item) {
            //获取子项对应的层级标记-数组
            var dataIndex = item.attr("data-index");
            return dataIndex.split(this.symbolReg);
        },
        // --- 获取元素层级标记
        getEleIndexArr:function(item){
            var dataEleIndex = item.attr("data-ele-index");
            return dataEleIndex.split(this.symbolReg);
        },
        // --- 追踪元素层级标记(根据当前显示的页面)
        catchEleIndexArrByPage(item){
            this.eleByMatchPage = null;
            this.eleByMatchPage = this.getEleIndexArr(item);
            //console.log("---追踪元素标记this.eleByMatchPage---",this.eleByMatchPage);
        },
        // --- 获取与菜单项对应的数据
        getItemData: function(navDatas, arr) {
            if($.isArray(navDatas) && navDatas.length){
                try {
                    //console.log(arr);
                    var index = parseInt(arr.shift()) - 1;
                    var tmpData = navDatas[index];
                    return !arr.length ? tmpData : this.getItemData(tmpData.children, arr);
                } catch (error) {
                    console.warn("---getItemData出错啦---",error);
                    return [];
                }
            }else{
                console.warn("---传入的菜单项数据不能为空哦，找不到对应的数据---",error);
                return [];
            }
        },
        // --- 获取与元素标记对应的菜单项jq(从第1级菜单开始找)
        getItemEle: function(navItems,cg){
            // --- cg: 指定获取哪一层元素标记
            var index,
                i = 0,
                resultEle,
                resultEleNextList;
            try {
                aaa.call(this,navItems);
                return resultEle;
                /* var index = this.eleByMatchPage.shift()*1 - 1;
                var resultEle = navItems.eq(index);
                var eleNextList = resultEle.next();
                return !this.eleByMatchPage.length?resultEle:this.getItemEle(eleNextList); */
            } catch (error) {
                console.warn("---getItemEle---",error);
                return $(this.settings.emptyClass);
            }
            // ---
            function aaa(jqEleList){
                //console.log("---i---",i);
                index = this.eleByMatchPage[i]*1 - 1;
                //console.log("---index---",index);
                if(_.isNaN(index)|| i>cg)return;
                resultEle = jqEleList.eq(index);
                resultEleNextList = resultEle.next().children(this.settings.itemClass);
                //console.log("---resultEleNextList---",resultEleNextList);
                i++;
                aaa.call(this,resultEleNextList);
            }
        },
        //获取与index对应的各项数据(按照层级顺序)
        getMatchedItemsByIndex: function(index) {
            //console.log("---index---",index); 
            var tmpArr = /\-/g.test(index) ? index.split("-") : [index];
            var results = [];
            //
            getName.call(this, this.treeData, tmpArr);
            //
            function getName(data, arr) {
                if (!arr.length || !data) return;
                var dataItem = data[arr.shift() * 1 - 1];
                //results.push(dataItem[this.settings.itemName]);
                var cloneDataItem = {};
                for (const key in dataItem) {
                    if (dataItem.hasOwnProperty(key)) {
                        if(!_.isArray(dataItem[key])){
                            cloneDataItem[key] = dataItem[key];
                        }else{
                            cloneDataItem[key] = dataItem[key].length;
                        }
                    }
                }
                results.push(cloneDataItem);
                //var children = dataItem.children && _.isArray(dataItem.children) ? dataItem.children : [];
                //console.log("---dataItem---",dataItem);
                //
                getName.call(this, dataItem.children, arr);
            }
            return results;
        },
        navItemIsActive: function() {
            //是否有is-active类名
            return this.hasClass(DsSideMenu.prototype.activeClassName);
        },
        navItemRemoveClass: function() {
            //删除类名
            this.removeClass(DsSideMenu.prototype.activeClassName);
        },
        // ---
        navItemAddClass: function() {
            //添加类名
            this.siblings()
                .removeClass(DsSideMenu.prototype.activeClassName)
                .end()
                .addClass(DsSideMenu.prototype.activeClassName);
        },
        // ---
        navItemListHide: function() {
            //隐藏菜单
            return this.stop(true, true)
                .slideUp(200)
                .promise();
        },
        // ---
        navItemListShow: function() {
            //显示菜单
            return this.stop(true, true)
                .slideDown(200)
                .promise();
        },
        // 是不是菜单元素
        isNavList: function() {
            //是否是菜单
            return this.is(DsSideMenu.prototype.menuClass);
        },
        // 根据1级菜单列表的收起展开计算竖向滚动距离
        recalcVerticalValues(navItem){
            var level = navItem.attr("data-level");
            if(level != 1)return false;
            //alert("10086___recalcVerticalValues");
            //
            this.getVerticalValues();
            //
            var offsetInfo =  navItem.offset();
            //console.info("---navItem.offset()---",offsetInfo);
            var realTop = offsetInfo.top - this.settings.posTop;
            // 有滚动距离
            if( this.NLWST > 0 ){
                if( realTop <= this.NLWST ) {
                    this.stepDis = 0;
                } else {
                    // realTop / this.LWH = x / this.NLWST;
                    this.stepDis = Math.round((realTop / this.NLWH)*this.NLWST)*-1;
                }
                this.listWrapTempSlide(this.stepDis);
            }
        },
        // --- 菜单项（收起<->展开）
        toggleMenuItem: function(navItem) {
            var _this = this;
            //var level = navItem.attr("data-level");
            //展开子项的菜单
            var itemNavList = navItem.next(); //对应的菜单
            if (this.navItemIsActive.call(navItem)) {
                if (this.isNavList.call(itemNavList)) {
                    this.navItemRemoveClass.call(navItem);
                    //收起菜单
                    this.navItemListHide.call(itemNavList).then(function(){
                        // 1级菜单项
                        _this.recalcVerticalValues(navItem);
                    });
                }
            } else {
                this.navItemAddClass.call(navItem);
                if (this.isNavList.call(itemNavList)) {
                    //展开菜单
                    this.navItemListShow.call(itemNavList).then(function() {
                        // --- 
                        _this.resetSubMenuPos(navItem,itemNavList);
                        /* // --- 获取对应的元素编号
                        var eleIndexInfo = _this.getEleIndexArr(navItem);
                        //console.log("---eleIndexInfo---",eleIndexInfo);
                        // --- 展开项的位置信息
                        var itemPos = navItem.position();
                        //console.log("---navItem.position---",itemPos);
                        // --- 判断侧边栏是否收起
                        if(_this.sideBar.hasClass(_this.settings.toggleClass)){
                            itemNavList.css({
                                top: itemPos.top+ "px"
                            });
                        }else{
                            // --- 不是1级菜单项才调整子菜单的top值
                            if(eleIndexInfo.length > 1){
                                itemNavList.css({
                                    top: itemPos.top+ "px"
                                });
                            } 
                        } */
                        /* var posInfo = navItem.get(0).getBoundingClientRect(),
                            posInfo2 = navItem
                            .parent()
                            .get(0)
                            .getBoundingClientRect(),
                            indexInfo = _self.getEleIndexArr(navItem);
                        if (indexInfo.length >= 2) {
                            itemNavList.css({
                                top: posInfo.top - posInfo.height - posInfo2.top + "px"
                            });
                        } */
                        // 1级菜单项
                        _this.recalcVerticalValues(navItem);
                    });
                }
            }
        },
        // --- 调整子菜单的位置
        resetSubMenuPos(navItem,itemNavList){
            // --- 获取对应的元素编号
            var eleIndexInfo = this.getEleIndexArr(navItem);
            //console.log("---eleIndexInfo---",eleIndexInfo);
            // --- 展开项的位置信息
            var itemPos = navItem.position();
            //console.log("---navItem.position---",itemPos);
            // --- 判断侧边栏是否收起
            if(this.sideMenu.hasClass(this.settings.toggleClass)){
                itemNavList.css({
                    top: itemPos.top+ "px"
                });
            }else{
                // --- 不是1级菜单项才调整子菜单的top值
                if(eleIndexInfo.length > 1){
                    itemNavList.css({
                        top: itemPos.top+ "px"
                    });
                } 
            }
        },
        // --- 收起菜单
        hideOhterNavItemLists: function(navItems) {
            //隐藏其它子项的菜单
            $.each(
                navItems,
                $.proxy(function(i, e) {
                    this.hideNavItemList($(e));
                }, this)
            );
        },
        // --- 收起菜单（各级子菜单）
        hideNavItemList: function(navItem, noDeep) {
            // --- 要隐藏的子级菜单
            var oList = navItem.next();
            //清掉被选中的类名
            this.navItemRemoveClass.call(navItem);
            //是菜单的时候
            if (this.isNavList.call(oList)) {
                this.navItemListHide.call(oList).then($.proxy(function(){
                    // --- 隐藏子级下的菜单
                    !noDeep && this.hideOhterNavItemLists(oList.children(this.settings.itemClass));
                },this));
            }
        },
        // --- 获取第1级菜单元素
        getFLMenuItem: function() {
            this.FLMenuItem = null;
            this.FLMenuItem = this.listWrap.children(
                this.settings.itemClass + "[data-level=1]"
            );
            //console.log("---this.FLMenuItem---",this.FLMenuItem);
        },
        // --- 获取第1级菜单元素中被展开的元素
        getFLMenuActiveItem:function(){
            return this.FLMenuItem.filter("[data-level=1]"+this.settings.activeClass);
        },
        // --- 收起不是当前active菜单项的所有其它菜单(从第1级到最后一级)--点击菜单项
        toggleUpListByLevelX: function(item) {
            //this.getFLMenuItem();
            //var indexArr2 = this.getEleIndexArr(item);
            //console.log("---indexArr2---",indexArr2);
            //收起同级的其它所有子项菜单
            this.hideOhterNavItemLists(item.siblings(this.settings.itemClass));
            // this.hideOhterNavItemLists(
            //     this.FLMenuItem.not("[data-ele-index=" + indexArr2[0] + "]")
            // );
        },
        // --- 收起不是当前active菜单项所有其它1级菜单--点击菜单项
        toggleUpListByLevel1: function(item) {
            item.siblings(this.settings.itemClass).each(
                $.proxy(function(i, e) {
                    this.hideNavItemList($(e), true);
                }, this)
            );
        },
        //展开菜单
        openNavMenu: function(tabName) {
            //alert(tabName);
            //?
            if (/\?/.test(tabName)) {
                tabName = tabName.slice(0, tabName.indexOf("?"));
            }
            //根据tabName展开对应的菜单
            //第1级菜单元素
            this.getFLMenuItem();
            //找出对应的菜单数据
            //var matchedData, index;
            var eleIndexInfo = this.getEleIndexByTabName(this.treeData, tabName);
            //console.log("---eleIndexInfo---",eleIndexInfo);
            //找到对应的元素标记
            if (_.isNumber(eleIndexInfo.eleIndex)) {
                this.clearItemsEvents();
                // --- 收起已展开的菜单(有些子页面没渲染侧边栏子项，但数据中有该项数据)
                this.hideOhterNavItemLists(this.FLMenuItem);
                //是不是已经展开的菜单  ???
                /* var openedItem = this.FLMenuItem.filter(this.settings.activeClass);
                if (openedItem.length) {
                    alert("hahaha");
                    var indexArr = this.getIndexArr(openedItem);
                    var openedItemData = this.getItemData(this.treeData, indexArr);
                    //已展开的子项-图标
                    this.fns.itemIconChange.apply(this, [openedItem, openedItemData, true]);
                    //已展开的子项-箭头
                    this.fns.itemArrowChange.apply(this, [
                        openedItem,
                        openedItemData,
                        true
                    ]);
                } */
                //展开对应的子项 --- 从第1或2级开始展开菜单
                this.openIndex = this.eleIndexArrOfItem.length > 1 ? 1 : 0;
                // --- 根据元素标记从1级菜单元素开始展开
                this.openNavItemList(this.FLMenuItem.eq(eleIndexInfo.eleIndex));
                _.delay($.proxy(this.setItemsEvents, this), 200);
            } else {
                //alert("导航路径不存在");
            }
            return eleIndexInfo.dataPath?eleIndexInfo.dataPath:"";
        },
        // --- 获取要展开项的元素编号
        getEleIndexByTabName: function(data, tabName) {
            //获取要展开项的对应元素编号
            var eleIndex, // --- 元素标记
                dataPathIndex; // --- 数据标记
            this.eleIndexArrOfItem = null;
            for (var i = 0, len = data.length; i < len; i++) {
                var item = data[i];
                // --- 没有创建菜单项元素，直接跳过
                if(!item.eleIndex)continue;
                // --- 展开项有木有子级数据
                var children = (item.children && $.isArray(item.children))?item.children:[];
                // --- 当前路径匹配且（有 or 没有）子项
                if (
                    (item[this.settings.tabName] == tabName && !children.length)||
                    (item[this.settings.tabName] == tabName && children.length)
                    ) {
                    this.eleIndexArrOfItem =
                        item.eleIndex.search(this.symbolReg) != -1 ?
                        item.eleIndex.split("-") :
                        [item.eleIndex];
                    eleIndex = this.eleIndexArrOfItem[0]*1 - 1;
                    dataPathIndex = item.index;
                    break;
                }
                // --- 当前路径不匹配且有子项
                if (item[this.settings.tabName] != tabName && children.length) {
                    if (this.hasTabName(children, tabName)) {
                        eleIndex = this.eleIndexArrOfItem[0]*1 - 1;
                        dataPathIndex = this.eleIndexArrOfItem.tmpInfo.index;
                        break;
                    }
                }
            }
            return eleIndex !== undefined ? {
                eleIndex: eleIndex,  // --- 元素标记
                dataPath: dataPathIndex  // --- 数据标记
            } : {};
        },
        // ---
        hasTabName: function(data, tabName) {
            var dataIsArray = $.isArray(data);
            if (!dataIsArray) return false;
            var result = false;
            this.eleIndexArrOfItem = null;
            for (var i = 0, len = data.length; i < len; i++) {
                var item = data[i];
                if (item[this.settings.tabName] == tabName) {
                    //matched
                    //console.log(item[this.settings.tabName]);
                    this.eleIndexArrOfItem =
                        item.eleIndex.search(this.symbolReg) != -1 ?
                        item.eleIndex.split("-") :
                        [item.eleIndex];
                    // --- 存数据信息
                    this.eleIndexArrOfItem.tmpInfo = null;
                    this.eleIndexArrOfItem.tmpInfo = item;
                    result = true;
                    break;
                } else {
                    //console.log("tabName in children");
                    result = this.hasTabName(item.children, tabName);
                    if (result) {
                        break;
                    }
                }
            }
            return result;
        },
        // ---
        openNavItemList: function(item) {
            //展开  --- 只用判断到第2级菜单  [1-1]
            var openIndex = this.eleIndexArrOfItem[this.openIndex] * 1;
            if (!this.eleIndexArrOfItem || !openIndex || _.isNaN(openIndex)){
                // 获取竖向滚动的相关值
                this.getVerticalValues();
                return;
            }
            var level = item.attr("data-level");
            //console.log("---level---",level);
            // ---
            this.navItemAddClass.call(item);
            // --- 追踪元素标记
            this.catchEleIndexArrByPage(item);
            if (level == 1) {
                // --- 菜单项对应的数据
                var itemData = this.getItemData(this.treeData, this.getIndexArr(item));
                //已展开的子项-图标
                this.fns.itemIconChange.apply(this, [item, itemData, false]);
                //已展开的子项-箭头
                this.fns.itemArrowChange.apply(this, [item, itemData, false]);
            }
            var oList = item.next();
            if (this.isNavList.call(oList)) {
                this.navItemListShow.call(oList).then($.proxy(function(){
                    // 1级菜单项
                    this.recalcVerticalValues(item);
                    // --- 当前1级菜单项已经匹配到路径，只展开对应的下一级菜单（不递归）
                    if(this.eleIndexArrOfItem.length<=1){
                        // --- 调整子菜单位置
                        this.resetSubMenuPos(item,oList);
                        return;
                    }
                    // --- 调整子菜单位置
                    this.resetSubMenuPos(item,oList);
                    //要添加avctive类名的
                    var activeItem = oList
                        .children(this.settings.itemClass)
                        .eq(openIndex - 1);
                    // --- 追踪元素标记
                    this.catchEleIndexArrByPage(activeItem);
                    // --- 
                    this.toggleMenuItem(activeItem);
                    // --- 有下级菜单显示
                    this.openIndex++;
                    // --- 展开后面的子级菜单
                    this.openNavItemList(activeItem);
                },this));
            }
        },
        // ---
        clearIconSrc: function(icon) {
            //icon图标恢复成默认的src
            icon.attr("src", icon.attr("data-noselect"));
        },
        // ---
        clearArrowSrc: function(arrow) {
            //箭头图标恢复成默认的src
            arrow.attr("src", arrow.attr("data-noselect"));
        },
        getNextListChildren: function(navItem) {
            //获取菜单子元素
            return navItem.next().children(this.settings.itemClass);
        },
        getActiveNavItem: function(navItems, IA) {
            //获取菜单中有active类名的菜单项
            var activeNavItem;
            var index = IA.shift() * 1 - 1;
            activeNavItem = navItems.eq(index);
            if (!IA.length) {
                return activeNavItem;
            }
            var nextList = activeNavItem.next();
            if (IA.length && this.isNavList.call(nextList)) {
                return this.getActiveNavItem(
                    nextList.children(this.settings.itemClass),
                    IA
                );
            }
        },
        setWrapEleZindex: function() {
            //重新设置侧边栏的层级
            if (this.navListWrap.attr("style")) return;
            var zArr = this.navListWrap
                .siblings()
                .addBack()
                .map(function(i, item) {
                    return $(item).css("z-index");
                })
                .get();
            for (var i = 0, z;
                (z = zArr[i++]);) {
                if (z == "auto") {
                    z = 0;
                }
                z = z * 1;
            }
            var maxZ = Math.max.apply(null, zArr);
            this.navListWrap.css("z-index", maxZ + 1);
        },
        clearItemsEvents: function() {
            //解绑侧边栏子项事件
            //this.listWrap.off("click", this.settings.itemClass);
            //this.navListWrap.siblings().off("click");
        },
        // --- 点击侧边栏之外的区域时,收起已展开的侧边栏菜单项(直接收起1级菜单)
        hideActiveMenuItem(){
            try {
                // --- 侧边栏是否折叠
                if(this.sideMenu.hasClass(this.settings.toggleClass)){
                    // --- 直接收起展开的1级菜单项
                    this.toggleMenuItem(this.getFLMenuActiveItem());
                }else{
                    // --- 当前显示项的元素标记
                    if(this.eleByMatchPage.length && this.eleByMatchPage.length >= 2){
                        // --- 收起2级菜单之后的菜单
                        //this.toggleMenuItem(this.getItemEle(this.FLMenuItem));
                        this.hideNavItemList(this.getItemEle(this.FLMenuItem,1));
                    }
                }
            } catch (error) {
                console.warn("---hideActiveMenuItem出错啦---",error);  
            }
        },
        // --- 侧边栏绑定事件
        setItemsEvents: function() {
            //绑定侧边栏事件
            this.listWrap.off("click").on(
                "click",
                this.settings.itemClass,
                $.proxy(function(ev) {
                    if (this.clickedControl) return false;
                    this.clickedControl = true;
                    //
                    //console.time("test");
                    //
                    var _this = $(ev.currentTarget);
                    var type = _this.attr("data-level");
                    //获取子项对应的层级标记-数组
                    var indexArr = this.getIndexArr(_this);
                    //console.log("---indexArr---",indexArr);
                    //是否有子级菜单
                    //var isList = this.isNavList.call(_this.next());
                    //是否有is-active类名
                    var isActive = this.navItemIsActive.call(_this);
                    //获取子项对应的数据
                    var matchedData = this.getItemData(this.treeData, indexArr);
                    //console.log("---matchedData---", matchedData);
                    // --- 操作图标和箭头 + 展开点击项 + 收起其它菜单
                    if(!isActive){
                        if (type == 1) {
                            // --- 1级菜单
                            this.fns.itemIconChange.apply(this, [_this, matchedData, isActive]);
                            this.fns.itemArrowChange.apply(this, [_this, matchedData, isActive]);
                            // --- 
                            if (
                                $.isArray(matchedData.children) && 
                                matchedData.children.length &&
                                !matchedData[this.settings.tabName]
                                ) {
                                //第一级菜单-只收起同级的第一级菜单
                                this.toggleUpListByLevel1(_this);
                            } else {
                                this.toggleUpListByLevelX(_this);
                            }
                        } else {
                            this.toggleUpListByLevelX(_this);
                        }
                        //触发点击后的方法 ->
                        this.fns.itemClickAfter.call(this, matchedData);
                    }else{
                        if(type == 1){
                            // --- 1级菜单
                            this.fns.itemIconChange.apply(this, [_this, matchedData, isActive]);
                            this.fns.itemArrowChange.apply(this, [_this, matchedData, isActive]);
                        }
                        // --- 
                        if(matchedData[this.settings.tabName]){
                            this.hideOhterNavItemLists(_this.next().children());
                        }
                    }
                    // --- 展开或收起对应菜单
                    this.toggleMenuItem(_this);
                    // --- 追踪元素标记
                    this.catchEleIndexArrByPage(_this);
                    /*if (!isList && !isActive) {
                        //this.fns.itemClickAfter.call(this, matchedData);
                        var indexArr2 = this.getEleIndexArr(_this),
                            FLItem = this.FLMenuItem.eq(indexArr2[0] * 1 - 1);
                        console.log("---indexArr2---",indexArr2);
                        console.log("---FLItem---",FLItem);
                        // 菜单是否折叠
                        if (this.sideBar.hasClass(this.settings.toggleClass)) {
                            if (indexArr2.length >= 2) {
                                var IA = this.getIndexArr(FLItem);
                                var matchedData = this.getItemData(this.treeData, IA);
                                this.fns.itemIconChange.apply(this, [FLItem, matchedData, true]);
                                this.fns.itemArrowChange.apply(this, [FLItem, matchedData, true]);
                                //
                                this.toggleMenuItem(FLItem);
                            }
                        } else {
                            if (indexArr2.length >= 3) {
                                var sActiveItem = this.getActiveNavItem(
                                    this.FLMenuItem,
                                    indexArr2.splice(0, 2)
                                );
                                this.toggleMenuItem(sActiveItem);
                            }
                        }
                    } */
                    //clickedControl
                    this.clickedControl = false;
                    //
                    //console.timeEnd("test");
                }, this)
            );
            //取消侧边栏层级设置
            /* var navParent = this.navListWrap.parent();
            var navPP = navParent.parent();
            // console.log("------",navParent.siblings().add(navParent.parent().prev()));
            var sibs = null;
            // --- 侧边栏外有父级（页面有多个菜单时）
            if(navPP.length){
                sibs = navPP.length ? navParent.siblings().add(navPP.prev()) : navParent.siblings();
            }    
            sibs
            .off("click")
                .on(
                    "click",
                    $.proxy(function(ev) {
                        //console.log("------",this.navListWrap.parent().siblings());
                        //ev.stopPropagation();
                        
                    }, this)
                ); */
            // --- 点击侧边栏之外的区域时,收起已展开的侧边栏菜单项
            $(document).off("click").on(
                "click",
                ".zz-header,.zz-pagetabs",
                $.proxy(function(ev){
                    this.hideActiveMenuItem();
                    //this.sideBar.hasClass(this.settings.toggleClass) && this.toggleMenuItem(this.getFLMenuActiveItem())
                    return false;
                    /* if (this.FLMenuItem) {
                        var FLactive = this.FLMenuItem.filter(this.settings.activeClass);
                        if (!FLactive.length) return false;
                        if (this.sideBar.hasClass(this.settings.toggleClass)) {
                            this.hideNavItemList(FLactive);
                            var indexArr = this.getIndexArr(FLactive);
                            var matchedData = this.getItemData(this.treeData, indexArr);
                            FLactive.removeClass(this.settings.activeClass.slice(1));
                            this.fns.itemIconChange.apply(this, [
                                FLactive,
                                matchedData,
                                true
                            ]);
                            this.fns.itemArrowChange.apply(this, [
                                FLactive,
                                matchedData,
                                true
                            ]);

                        } else {
                            var nac = this.getNextListChildren(FLactive).filter(
                                this.settings.activeClass
                            );
                            if (nac.length && this.isNavList.call(nac.next())) {
                                this.toggleMenuItem(nac);
                            }
                        }
                    } */
                },this)
            );
            // --- 折叠侧边栏
            this.sideToggleBtn.off("click").on("click",$.proxy(function(){
                // --- 侧边栏折叠???
                this.sideMenu.toggleClass(this.settings.toggleClass);
                // --- 内容区布局调整
                var zzBodyArea = this.sideMenu.siblings(this.settings.pageContentClass);
                zzBodyArea && zzBodyArea.toggleClass(this.settings.pCCSideMenuToggle);
                // --- 调整tab栏的滑动距离和位置
                // --- 计算tabList的滑动距离
                this.tabBarObj.calcTabListSlideInfo();
                // --- tabList滑动到当前显示的tabitem
                this.tabBarObj.tabListSlideByCur();
                // --- 调整内容区的位置
                //alert(this.tabBarObj);
            },this));
            // --- 侧边栏容器的滚轮事件
            this.navListWrap.off("mousewheel").on("mousewheel",_.debounce($.proxy(function(e){
                //console.log("---navListWrap__mousewheel---",e.originalEvent);
                this.navListWrapScrollFn(e,"deltaY",function(deltaY){
                    return Math.abs(deltaY)/10;
                });
            },this),16));
            // --- 侧边栏容器的滚轮事件
            this.navListWrap.off("DOMMouseScroll").on("DOMMouseScroll",_.debounce($.proxy(function(e){
                console.log("---navListWrap__DOMMouseScroll---",e.originalEvent);
                this.navListWrapScrollFn(e,"detail",function(deltaY){
                    return Math.abs(deltaY)*3;
                });
            },this),16));
        },
        // --- 侧边栏滚动
        navListWrapScrollFn:function(e,propName,fn){
            var deltaY = e.originalEvent[propName];
            //var stepY = Math.abs(deltaY)/10;
            //Math.abs(deltaY)/10
            var stepY = fn && fn(deltaY);
            //console.log("---stepY---",stepY);
            // 向下滚动
            if( deltaY > 0 ){
                //console.log("---向下滚动---",(stepY*-1));
                if(Math.abs(this.stepDis)>=this.NLWST){
                    this.stepDis = this.NLWST*-1;
                    this.listWrapTempSlide(this.stepDis);
                    return false;
                }
                this.stepDis+=(stepY*-1);
            }
            // 向上滚动
            if( deltaY < 0 ){
                //console.log("---向上滚动---",stepY);
                if(this.stepDis>=0){
                    this.stepDis = 0;
                    this.listWrapTempSlide(this.stepDis);
                    return false;
                }
                this.stepDis+=stepY;
            }
            //this.listWrapTemp.css("transform","translateY("+this.stepDis+"px)");
            this.listWrapTempSlide(this.stepDis);
        },
        // --- 侧边栏隐藏
        hideSideMenu(){
            // --- 
            var zzBodyArea = this.sideMenu.siblings(this.settings.pageContentClass);
            if(!zzBodyArea.length)return $.Deferred();
            this.toggleMenuItem(this.getFLMenuActiveItem());
            return this.sideMenu.stop(true,true).animate({
                "left":-1*this.sideMenuWidth+"px"
            }).promise().done($.proxy(function(){
                // --- 内容区布局调整
                zzBodyArea.addClass(this.settings.pCCSideMenuHide);
            },this));
        },
        // --- 侧边栏显示
        showSideMenu(){
            // --- 
            var zzBodyArea = this.sideMenu.siblings(this.settings.pageContentClass);
            if(!zzBodyArea.length)return $.Deferred();
            return this.sideMenu.stop(true,true).animate({
                "left":"0px"
            }).promise().done($.proxy(function(){
                // --- 内容区布局调整
                zzBodyArea.removeClass(this.settings.pCCSideMenuHide);
            },this));
        }
    };
    window.zzSideMenu = DsSideMenu;
})();